package com.loyal.loyalerp.basedata.permission.model.vo;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.PageInfo;

public class GridVO<T> {
	private Integer page;
	private Integer total;
	private Long records;
	private List<RowVO<T>> rows;
	
	public static final <T> GridVO<T> fromPageInfo(PageInfo<T> info) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		return fromPageInfo(info, "id");
	}
	public static final <T> GridVO<T> fromPageInfo(PageInfo<T> info,String idAttributeName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		GridVO<T> grid = new GridVO<T>();
		grid.setPage(info.getPageNum());// 当前页
		grid.setRecords(info.getTotal());// 总记录数
		grid.setTotal(info.getPages());// 总页数
		List<RowVO<T>> rows = new ArrayList<>();
		List<T> list = info.getList();
		if (list.size() > 0) {
			Field field = list.get(0).getClass().getDeclaredField(idAttributeName);
			field.setAccessible(true);
			for (T o : info.getList()) {
				RowVO<T> row = new RowVO<T>();
				Long id = ((Number)field.get(o)).longValue();
				row.setId(id);
				row.setCell(o);
				rows.add(row);
			}
		}
		grid.setRows(rows);// 数据
		return grid;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public Long getRecords() {
		return records;
	}

	public void setRecords(Long records) {
		this.records = records;
	}

	public List<RowVO<T>> getRows() {
		return rows;
	}

	public void setRows(List<RowVO<T>> rows) {
		this.rows = rows;
	}
	private static class RowVO<T>{
		public Long id;
		public T cell;
		@SuppressWarnings("unused")
		public Long getId() {
			return id;
		}
		public void setId(Long id) {
			this.id = id;
		}
		@SuppressWarnings("unused")
		public T getCell() {
			return cell;
		}
		public void setCell(T cell) {
			this.cell = cell;
		}
	}
	
}
